﻿#include "DBOperate.h"
#include "SCommon.h"
#include <QMessageBox>
#include <QDateTime>
#include  "Pacs_User.h"
#include "PacsClient.h"


DBOperate * DBOperate::m_instance = NULL;
DBOperate::DBOperate()
{
	m_dbUtil = DButils::GetKernel();
}

DBOperate * DBOperate::GetKernel()
{
	if (m_instance == NULL)
	{
		m_instance = new DBOperate();
	}
	return m_instance;
}
bool DBOperate::DBconnect()
{
	m_dbUtil->CloseDb();
	QString strPath = SCommon::getCurrExePath()+"/db";
	bool isHaveDir = SCommon::isDirExits(strPath );
	if (!isHaveDir)
	{
		bool isSus = SCommon::createDir(strPath + "/db");
		if (!isSus)
		{
			QMessageBox::warning(NULL, QString::fromLocal8Bit("PacsWarnning"), QString::fromLocal8Bit("创建数据库文件夹失败!"));
			return false;
		}
	}
	m_dbUtil->SetDatabaseName(strPath + "/pacs.db");
	if (!m_dbUtil->OpenDb())
	{
		QMessageBox::warning(NULL, QString::fromLocal8Bit("PacsWarnning"), QString::fromLocal8Bit("数据库打开失败"));
		return false;
	}
	return true;
}

QList<CheckPart>  DBOperate::getUserCheckPart()
{
	QList<CheckPart> checkPartList;

	QString strSql = "SELECT id, partName,userId FROM userCheckPart WHERE userId = %1 or userId = 0";
	strSql = strSql.arg(0);
	QSqlQuery sqlQuery = m_dbUtil->excuteSql(strSql);
	// 解析结果
	while (sqlQuery.next())
	{
		CheckPart checkPart;
		checkPart.id = sqlQuery.value("id").toInt();
		checkPart.strPartName = sqlQuery.value("partName").toString();
		checkPartList.append(checkPart);
	}
	return checkPartList;
}

QList<CheckPart> DBOperate::getShowCheckPart()
{
	QList<CheckPart> checkPartList;

	QString strSql = "SELECT id, showName,type FROM showCheckPart";
	QSqlQuery sqlQuery = m_dbUtil->excuteSql(strSql);
	// 解析结果
	while (sqlQuery.next())
	{
		CheckPart checkPart;
		checkPart.id = sqlQuery.value("id").toInt();
		checkPart.strPartName = sqlQuery.value("showName").toString();
		checkPartList.append(checkPart);
	}
	return checkPartList;
}
QList<CheckPart> DBOperate::getCheckPartList()
{
	QList<CheckPart> checkPartList;

	QString strSql = "SELECT ID, Name FROM CheckPart";
	QSqlQuery sqlQuery = m_dbUtil->excuteSql(strSql);
	// 解析结果
	while (sqlQuery.next())
	{
		CheckPart checkPart;
		checkPart.id = sqlQuery.value("ID").toInt();
		checkPart.strPartName = sqlQuery.value("Name").toString();
		checkPartList.append(checkPart);
	}
	return checkPartList;
}

void DBOperate::getCurrentPatientOrderNum()
{
	QString strSql = "SELECT todaypatient,lastAddPatientDate FROM config";
	QSqlQuery sqlQuery = m_dbUtil->excuteSql(strSql);
	qint64 lastDate = 0;
	int currentOrderNum = 0;
	// 解析结果
	while (sqlQuery.next())
	{
		int lastOrderNum = sqlQuery.value("todaypatient").toInt();
		lastDate = sqlQuery.value("lastAddPatientDate").toULongLong();
		qint64 nowDate = PacsClient::getClientInstance()->GetNowTodayTime();
		if (lastDate < nowDate) {
			currentOrderNum = 0;
		}
		else
		{ 
			currentOrderNum = lastOrderNum;
		}
	}
	PacsClient::getClientInstance()->SetTodayOrderNum(currentOrderNum);
}
void  DBOperate::updateTodayOrderNumConfig()
{
	int currentOrderNum = PacsClient::getClientInstance()->GetCurrentTodayOrderNum();
	qint64 nowDate = PacsClient::getClientInstance()->GetNowTodayTime();
	QString strSql = "UPDATE config SET todaypatient = %1,lastAddPatientDate = %2";
	strSql = strSql.arg(QString::number(currentOrderNum)).arg(QString::number(nowDate));
	m_dbUtil->excuteSql(strSql);
}
QList<PartModel> DBOperate::getPartModelList()
{
	QList<PartModel> modelList;
	QString strSql = "SELECT ID,Name,ModSee,ModDia,CheckTypeID,UserID FROM Model WHERE UserID=0";
	QSqlQuery sqlQuery = m_dbUtil->excuteSql(strSql);
	// 解析结果
	while (sqlQuery.next())
	{
		PartModel partModel;
		partModel.id = sqlQuery.value("ID").toInt();
		partModel.modelName = sqlQuery.value("Name").toString();
		partModel.modeSee = sqlQuery.value("ModSee").toString();
		partModel.modeRes = sqlQuery.value("ModDia").toString();
		partModel.checkTypeId = sqlQuery.value("CheckTypeID").toInt();
		partModel.userId = sqlQuery.value("UserID").toInt();
		modelList.append(partModel);
	}
	return modelList;
}

void DBOperate::GetNoCheckPatient()
{
	qint64 nowDateSeconds = PacsClient::getClientInstance()->GetNowTodayTime();
	QString strSql = "SELECT CheckNum,ArchivesNum,Name, Sex,Age,AgeUnit,Height,Weight,Tell,\
										  Identity,Job,Addr,CheckType,ZhuYuanNum,BedNum,SongDoor,SongDoctor,\
										 LinChuangCheck,PatientTell,YuYueTime,ApplyNum,CheckPart,PhoneNum,checkStatus,filePath,history FROM patient WHERE YuYueTime > %1 AND checkStatus < 2 ORDER BY YuYueTime";
	strSql = strSql.arg(QString::number(nowDateSeconds));
	QSqlQuery sqlQuery = m_dbUtil->excuteSql(strSql);
	while (sqlQuery.next())
	{
		QString checkNum = sqlQuery.value("CheckNum").toString();
		if (checkNum.trimmed().isEmpty())
		{
			continue;
		}
		QString ArchivesNum = sqlQuery.value("ArchivesNum").toString();
		QString name = sqlQuery.value("Name").toString();
		QString sex = sqlQuery.value("Sex").toString();
		int  age = sqlQuery.value("Age").toInt();
		QString AgeUnit = sqlQuery.value("AgeUnit").toString();
		double Height = sqlQuery.value("Height").toDouble();
		double weight = sqlQuery.value("Weight").toDouble();
		QString tell = sqlQuery.value("Tell").toString();
		QString Identity = sqlQuery.value("Identity").toString();
		QString Job = sqlQuery.value("Job").toString();
		QString Addr = sqlQuery.value("Addr").toString();
		QString CheckType = sqlQuery.value("CheckType").toString();
		QString ZhuYuanNum = sqlQuery.value("ZhuYuanNum").toString();
		QString BedNum = sqlQuery.value("BedNum").toString();
		QString SongDoor = sqlQuery.value("SongDoor").toString();
		QString SongDoctor = sqlQuery.value("SongDoctor").toString();
		QString LinChuangCheck = sqlQuery.value("LinChuangCheck").toString();
		QString PatientTell = sqlQuery.value("PatientTell").toString();
		uint64 YuYueTime = sqlQuery.value("YuYueTime").toULongLong();
		QString ApplyNum = sqlQuery.value("ApplyNum").toString();
		QString CheckPart = sqlQuery.value("CheckPart").toString();
		int PhoneNum = sqlQuery.value("PhoneNum").toInt();
		int checkStatus = sqlQuery.value("checkStatus").toInt();
		QString filePath = sqlQuery.value("filePath").toString();
		QString history = sqlQuery.value("history").toString();
		Pacs_Patient *patient = new Pacs_Patient;

		patient->setStrCheckNum(checkNum);
		patient->setFilePath(filePath);
		patient->setStrPatientName(name);
		patient->setStrPatientSex(sex);
		patient->setNPatientAge(age);
		patient->setStrAgeUnit(AgeUnit);
		patient->setStrCheckType(CheckType);
		patient->setStrZhuYuanNum(ZhuYuanNum);
		patient->setStrBedNum(BedNum);
		patient->setStrArchivesNum(ArchivesNum);
		patient->setStrSongDoor(SongDoor);
		patient->setStrSongDoctor(SongDoctor);
		patient->setNYuYueTime(YuYueTime);
		patient->setStrApplyNum(ApplyNum);
		patient->setNPhoneNum(PhoneNum);
		patient->setStrLinChuangCheck(LinChuangCheck);
		patient->setStrPatientTell(PatientTell);
		patient->setStrCheckPart(CheckPart);
		patient->setStrSickHistory(history);
		patient->setFHeight(Height);
		patient->setFWeight(weight);
		patient->setPatientStatus(PatientStatus(checkStatus));
		PacsClient::getClientInstance()->OnAddNewPatientInQueue(patient);
	}
}
void  DBOperate::GetTodayHangUpPatient()
{
	qint64 nowDateSeconds = PacsClient::getClientInstance()->GetNowTodayTime();
	QString strSql = "SELECT CheckNum,ArchivesNum,Name, Sex,Age,AgeUnit,Height,Weight,Tell,\
										  Identity,Job,Addr,CheckType,ZhuYuanNum,BedNum,SongDoor,SongDoctor,\
										 LinChuangCheck,PatientTell,YuYueTime,ApplyNum,CheckPart,PhoneNum,checkStatus,filePath,history FROM patient WHERE YuYueTime > %1 AND checkStatus = 6 ORDER BY YuYueTime";
	strSql = strSql.arg(QString::number(nowDateSeconds));
	QSqlQuery sqlQuery = m_dbUtil->excuteSql(strSql);
	
		while (sqlQuery.next())
		{
			QString checkNum = sqlQuery.value("CheckNum").toString();
			if (checkNum.trimmed().isEmpty())
			{
				continue;
			}
			QString ArchivesNum = sqlQuery.value("ArchivesNum").toString();
			QString name = sqlQuery.value("Name").toString();
			QString sex = sqlQuery.value("Sex").toString();
			int  age = sqlQuery.value("Age").toInt();
			QString AgeUnit = sqlQuery.value("AgeUnit").toString();
			double Height = sqlQuery.value("Height").toDouble();
			double weight = sqlQuery.value("Weight").toDouble();
			QString tell = sqlQuery.value("Tell").toString();
			QString Identity = sqlQuery.value("Identity").toString();
			QString Job = sqlQuery.value("Job").toString();
			QString Addr = sqlQuery.value("Addr").toString();
			QString CheckType = sqlQuery.value("CheckType").toString();
			QString ZhuYuanNum = sqlQuery.value("ZhuYuanNum").toString();
			QString BedNum = sqlQuery.value("BedNum").toString();
			QString SongDoor = sqlQuery.value("SongDoor").toString();
			QString SongDoctor = sqlQuery.value("SongDoctor").toString();
			QString LinChuangCheck = sqlQuery.value("LinChuangCheck").toString();
			QString PatientTell = sqlQuery.value("PatientTell").toString();
			uint64 YuYueTime = sqlQuery.value("YuYueTime").toULongLong();
			QString ApplyNum = sqlQuery.value("ApplyNum").toString();
			QString CheckPart = sqlQuery.value("CheckPart").toString();
			int PhoneNum = sqlQuery.value("PhoneNum").toInt();
			int checkStatus = sqlQuery.value("checkStatus").toInt();
			QString filePath = sqlQuery.value("filePath").toString();
			QString history = sqlQuery.value("history").toString();
			Pacs_Patient *patient = new Pacs_Patient;

			patient->setStrCheckNum(checkNum);
			patient->setFilePath(filePath);
			patient->setStrPatientName(name);
			patient->setStrPatientSex(sex);
			patient->setNPatientAge(age);
			patient->setStrAgeUnit(AgeUnit);
			patient->setStrCheckType(CheckType);
			patient->setStrZhuYuanNum(ZhuYuanNum);
			patient->setStrBedNum(BedNum);
			patient->setStrArchivesNum(ArchivesNum);
			patient->setStrSongDoor(SongDoor);
			patient->setStrSongDoctor(SongDoctor);
			patient->setNYuYueTime(YuYueTime);
			patient->setStrApplyNum(ApplyNum);
			patient->setNPhoneNum(PhoneNum);
			patient->setStrLinChuangCheck(LinChuangCheck);
			patient->setStrPatientTell(PatientTell);
			patient->setStrCheckPart(CheckPart);
			patient->setStrSickHistory(history);
			patient->setFHeight(Height);
			patient->setFWeight(weight);
			patient->setPatientStatus(PatientStatus(checkStatus));
			PacsClient::getClientInstance()->OnAddNewPatientHangUp(patient);
		}
}

void  DBOperate::GetTodayAllPatient()
{

	qint64 nowDateSeconds = PacsClient::getClientInstance()->GetNowTodayTime();

	QString strSql = "SELECT CheckNum,ArchivesNum,Name, Sex,Age,AgeUnit,Height,Weight,Tell,\
										  Identity,Job,Addr,CheckType,ZhuYuanNum,BedNum,SongDoor,SongDoctor,\
										 LinChuangCheck,PatientTell,YuYueTime,ApplyNum,CheckPart,PhoneNum,checkStatus,filePath,history FROM patient WHERE YuYueTime > %1 ORDER BY YuYueTime";
	strSql = strSql.arg(QString::number(nowDateSeconds));
	QSqlQuery sqlQuery = m_dbUtil->excuteSql(strSql);
	while (sqlQuery.next())
	{
		QString checkNum = sqlQuery.value("CheckNum").toString();
		if (checkNum.trimmed().isEmpty())
		{
			continue;
		}
		QString ArchivesNum = sqlQuery.value("ArchivesNum").toString();
		QString name = sqlQuery.value("Name").toString();
		QString sex = sqlQuery.value("Sex").toString();
		int  age = sqlQuery.value("Age").toInt();
		QString AgeUnit = sqlQuery.value("AgeUnit").toString(); 
		double Height = sqlQuery.value("Height").toDouble();
		double weight = sqlQuery.value("Weight").toDouble();
		QString tell = sqlQuery.value("Tell").toString(); 
		QString Identity = sqlQuery.value("Identity").toString(); 
		QString Job = sqlQuery.value("Job").toString();
		QString Addr = sqlQuery.value("Addr").toString();
		QString CheckType = sqlQuery.value("CheckType").toString();
		QString ZhuYuanNum = sqlQuery.value("ZhuYuanNum").toString();
		QString BedNum = sqlQuery.value("BedNum").toString();
		QString SongDoor = sqlQuery.value("SongDoor").toString();
		QString SongDoctor = sqlQuery.value("SongDoctor").toString();
		QString LinChuangCheck = sqlQuery.value("LinChuangCheck").toString();
		QString PatientTell = sqlQuery.value("PatientTell").toString();
		uint64 YuYueTime = sqlQuery.value("YuYueTime").toULongLong();
		QString ApplyNum = sqlQuery.value("ApplyNum").toString();
		QString CheckPart = sqlQuery.value("CheckPart").toString();
		int PhoneNum = sqlQuery.value("PhoneNum").toInt();
		int checkStatus = sqlQuery.value("checkStatus").toInt(); 
		QString filePath = sqlQuery.value("filePath").toString();
		QString history = sqlQuery.value("history").toString();
		Pacs_Patient *patient = new Pacs_Patient;

		patient->setStrCheckNum(checkNum);
		patient->setFilePath(filePath);
		patient->setStrPatientName(name);
		patient->setStrPatientSex(sex);
		patient->setNPatientAge(age);
		patient->setStrAgeUnit(AgeUnit);
		patient->setStrCheckType(CheckType);
		patient->setStrZhuYuanNum(ZhuYuanNum);
		patient->setStrBedNum(BedNum);
		patient->setStrArchivesNum(ArchivesNum);
		patient->setStrSongDoor(SongDoor);
		patient->setStrSongDoctor(SongDoctor);
		patient->setNYuYueTime(YuYueTime);
		patient->setStrApplyNum(ApplyNum);
		patient->setNPhoneNum(PhoneNum);
		patient->setStrLinChuangCheck(LinChuangCheck);
		patient->setStrPatientTell(PatientTell);
		patient->setStrCheckPart(CheckPart);
		patient->setStrSickHistory(history);
		patient->setFHeight(Height);
		patient->setFWeight(weight);
		patient->setPatientStatus(PatientStatus(checkStatus));
		PacsClient::getClientInstance()->OnAddNewPatient(patient);
		switch (patient->enumPatientStatus())
		{
		case PATIENT_NONE:
			PacsClient::getClientInstance()->OnAddNewPatientInQueue(patient);
			break;
		case PATIENT_INQUEUE:
			PacsClient::getClientInstance()->OnAddNewPatientInQueue(patient);
			break;
		case PATIENT_CHECKING:
		{
			PacsClient::getClientInstance()->OnAddNewPatientChecked(patient);
			PacsClient::getClientInstance()->OnAddNewPatientChecking(patient);
			GetPatientReprotInfo(patient);
		}
			break;
		case PATIENT_SAVEREPORT:
		{
			PacsClient::getClientInstance()->OnAddNewPatientChecked(patient);
			PacsClient::getClientInstance()->OnAddNewPatientChecking(patient);
			GetPatientReprotInfo(patient);
		}
			break;
		case PATIENT_PRINT:
		{
			PacsClient::getClientInstance()->OnAddNewPatientChecked(patient);
			PacsClient::getClientInstance()->OnAddNewPatientChecking(patient);
			GetPatientReprotInfo(patient);
		}
			
			break;
		case PATIENT_CHECKEND:
			PacsClient::getClientInstance()->OnAddNewPatientChecked(patient);
			break;
		case PATIENT_HANGUP:
			PacsClient::getClientInstance()->OnAddNewPatientHangUp(patient);
			break;
		default:
			break;
		}
	}
}

void  DBOperate::GetTodayAllCheckedPatient()
{
	qint64 nowDateSeconds = PacsClient::getClientInstance()->GetNowTodayTime();

	QString strSql = "SELECT CheckNum,ArchivesNum,Name, Sex,Age,AgeUnit,Height,Weight,Tell,\
										  Identity,Job,Addr,CheckType,ZhuYuanNum,BedNum,SongDoor,SongDoctor,\
										 LinChuangCheck,PatientTell,YuYueTime,ApplyNum,CheckPart,PhoneNum,checkStatus,filePath,history FROM patient WHERE  YuYueTime > %1 AND checkStatus BETWEEN 2 and 5  ORDER BY YuYueTime";
	strSql = strSql.arg(QString::number(nowDateSeconds));
	QSqlQuery sqlQuery = m_dbUtil->excuteSql(strSql);
	while (sqlQuery.next())
	{
		QString checkNum = sqlQuery.value("CheckNum").toString();
		if (checkNum.trimmed().isEmpty())
		{
			continue;
		}
		QString ArchivesNum = sqlQuery.value("ArchivesNum").toString();
		QString name = sqlQuery.value("Name").toString();
		QString sex = sqlQuery.value("Sex").toString();
		int  age = sqlQuery.value("Age").toInt();
		QString AgeUnit = sqlQuery.value("AgeUnit").toString();
		double Height = sqlQuery.value("Height").toDouble();
		double weight = sqlQuery.value("Weight").toDouble();
		QString tell = sqlQuery.value("Tell").toString();
		QString Identity = sqlQuery.value("Identity").toString();
		QString Job = sqlQuery.value("Job").toString();
		QString Addr = sqlQuery.value("Addr").toString();
		QString CheckType = sqlQuery.value("CheckType").toString();
		QString ZhuYuanNum = sqlQuery.value("ZhuYuanNum").toString();
		QString BedNum = sqlQuery.value("BedNum").toString();
		QString SongDoor = sqlQuery.value("SongDoor").toString();
		QString SongDoctor = sqlQuery.value("SongDoctor").toString();
		QString LinChuangCheck = sqlQuery.value("LinChuangCheck").toString();
		QString PatientTell = sqlQuery.value("PatientTell").toString();
		uint64 YuYueTime = sqlQuery.value("YuYueTime").toULongLong();
		QString ApplyNum = sqlQuery.value("ApplyNum").toString();
		QString CheckPart = sqlQuery.value("CheckPart").toString();
		int PhoneNum = sqlQuery.value("PhoneNum").toInt();
		int checkStatus = sqlQuery.value("checkStatus").toInt();
		QString filePath = sqlQuery.value("filePath").toString();
		QString history = sqlQuery.value("history").toString();
		Pacs_Patient *patient = new Pacs_Patient;

		patient->setStrCheckNum(checkNum);
		patient->setFilePath(filePath);
		patient->setStrPatientName(name);
		patient->setStrPatientSex(sex);
		patient->setNPatientAge(age);
		patient->setStrAgeUnit(AgeUnit);
		patient->setStrCheckType(CheckType);
		patient->setStrZhuYuanNum(ZhuYuanNum);
		patient->setStrBedNum(BedNum);
		patient->setStrArchivesNum(ArchivesNum);
		patient->setStrSongDoor(SongDoor);
		patient->setStrSongDoctor(SongDoctor);
		patient->setNYuYueTime(YuYueTime);
		patient->setStrApplyNum(ApplyNum);
		patient->setNPhoneNum(PhoneNum);
		patient->setStrLinChuangCheck(LinChuangCheck);
		patient->setStrPatientTell(PatientTell);
		patient->setStrCheckPart(CheckPart);
		patient->setStrSickHistory(history);
		patient->setFHeight(Height);
		patient->setFWeight(weight);
		patient->setPatientStatus(PatientStatus(checkStatus));
		PacsClient::getClientInstance()->OnAddNewPatientChecked(patient);
	}
}


QList<Pacs_Patient*> DBOperate::getSertchResPatient(quint64 srartTime,
													quint64 endTime, 
													const QString &strName,
													const QString & strCheckNum,
													bool isDianXing, 
													bool isBenDiZhenShi,
													bool isMyReport)
{
	QString strSql = " SELECT a.*,b.* FROM patient a LEFT JOIN Report b ON  a.CheckNum=b.CheckNum ";
	if (srartTime > 0 && endTime > 0)
	{
		strSql += " AND b.CheckDate BETWEEN " + QString::number(srartTime) + " AND " + QString::number(endTime);
	}
	strSql += "  WHERE 1=1 ";
	if (!strName.trimmed().isEmpty())
	{
		strSql += " AND a.Name = '"+strName+"'";
	}
	if (!strCheckNum.trimmed().isEmpty())
	{
		strSql += " AND a.CheckNum = '" + strCheckNum + "'";
	}
	if (isDianXing)
	{
		strSql += " AND a.isDianXing > 0";
	}
	if (isBenDiZhenShi)
	{
		
	}
	if (isMyReport)
	{
////////////////////等用户管理做完这块需添加/////////////////////////////////
	}

	
	strSql += " ORDER BY b.CheckDate ";
	QSqlQuery sqlQuery = m_dbUtil->excuteSql(strSql);
	QList<Pacs_Patient*> searchRes;
	while (sqlQuery.next())
	{
		QString checkNum = sqlQuery.value("CheckNum").toString();
		if (checkNum.trimmed().isEmpty())
		{
			continue;
		}
		QString ArchivesNum = sqlQuery.value("ArchivesNum").toString();
		QString name = sqlQuery.value("Name").toString();
		QString sex = sqlQuery.value("Sex").toString();
		int  age = sqlQuery.value("Age").toInt();
		QString AgeUnit = sqlQuery.value("AgeUnit").toString();
		double Height = sqlQuery.value("Height").toDouble();
		double weight = sqlQuery.value("Weight").toDouble();
		QString tell = sqlQuery.value("Tell").toString();
		QString Identity = sqlQuery.value("Identity").toString();
		QString Job = sqlQuery.value("Job").toString();
		QString Addr = sqlQuery.value("Addr").toString();
		QString CheckType = sqlQuery.value("CheckType").toString();
		QString ZhuYuanNum = sqlQuery.value("ZhuYuanNum").toString();
		QString BedNum = sqlQuery.value("BedNum").toString();
		QString SongDoor = sqlQuery.value("SongDoor").toString();
		QString SongDoctor = sqlQuery.value("SongDoctor").toString();
		QString LinChuangCheck = sqlQuery.value("LinChuangCheck").toString();
		QString PatientTell = sqlQuery.value("PatientTell").toString();
		uint64 YuYueTime = sqlQuery.value("YuYueTime").toULongLong();
		QString ApplyNum = sqlQuery.value("ApplyNum").toString();
		QString CheckPart = sqlQuery.value("CheckPart").toString();
		int PhoneNum = sqlQuery.value("PhoneNum").toInt();
		int checkStatus = sqlQuery.value("checkStatus").toInt();
		QString filePath = sqlQuery.value("filePath").toString();
		QString history = sqlQuery.value("history").toString();
		Pacs_Patient *patient = new Pacs_Patient;

		patient->setStrCheckNum(checkNum);
		patient->setFilePath(filePath);
		patient->setStrPatientName(name);
		patient->setStrPatientSex(sex);
		patient->setNPatientAge(age);
		patient->setStrAgeUnit(AgeUnit);
		patient->setStrCheckType(CheckType);
		patient->setStrZhuYuanNum(ZhuYuanNum);
		patient->setStrBedNum(BedNum);
		patient->setStrArchivesNum(ArchivesNum);
		patient->setStrSongDoor(SongDoor);
		patient->setStrSongDoctor(SongDoctor);
		patient->setNYuYueTime(YuYueTime);
		patient->setStrApplyNum(ApplyNum);
		patient->setNPhoneNum(PhoneNum);
		patient->setStrLinChuangCheck(LinChuangCheck);
		patient->setStrPatientTell(PatientTell);
		patient->setStrCheckPart(CheckPart);
		patient->setStrSickHistory(history);
		patient->setFHeight(Height);
		patient->setFWeight(weight);
		patient->setPatientStatus(PatientStatus(checkStatus));
		Pacs_Report* patientReport = patient->patientReport();
		patientReport->setStrCheckNum(sqlQuery.value("CheckNum1").toString());
		patientReport->setModeSee(sqlQuery.value("ModeSee").toString());
		patientReport->setModeRes(sqlQuery.value("ModeRes").toString());
		patientReport->setCheckDoctor(sqlQuery.value("CheckDoctor").toString());
		patientReport->setShenHeDoctor(sqlQuery.value("ShenHeDoctor").toString());
		patientReport->setCheckPart(sqlQuery.value("CheckPart1").toString());
		patientReport->setReportDate(sqlQuery.value("CheckDate").toULongLong());
		QString strAllImage = sqlQuery.value("PhotoPath").toString();
		patientReport->SetPatientAllImage(strAllImage.split(";"));
		QString  strSelectImage = sqlQuery.value("ReportPhotoPath").toString();
		patientReport->SetSelectImage(strSelectImage.split(";"));
		searchRes.append(patient);
	}
	return searchRes;
}

QList<CheckType> DBOperate::getCheckTypeList()
{
	QList<CheckType> checkTypeList;
	QString strSql = "SELECT ID,Name,CheckPartID FROM CheckType";
	QSqlQuery sqlQuery = m_dbUtil->excuteSql(strSql);
	while (sqlQuery.next())
	{
		CheckType checkType;
		checkType.id = sqlQuery.value("ID").toInt();
		checkType.strTypeName = sqlQuery.value("Name").toString();
		checkType.checkPartId = sqlQuery.value("CheckPartID").toInt();
		checkTypeList.append(checkType);
	}
	return checkTypeList;
}
void DBOperate::CreateNewPatient(Pacs_Patient* currentPatient)
{
	QString strSql = "INSERT INTO patient (CheckNum,ArchivesNum,Name, Sex,Age,AgeUnit,Height,Weight,Tell,\
										  Identity,Job,Addr,CheckType,ZhuYuanNum,BedNum,SongDoor,SongDoctor,\
										 LinChuangCheck,PatientTell,YuYueTime,ApplyNum,CheckPart,PhoneNum,checkStatus,filePath,history ) VALUES \
								         ('%1','%2','%3','%4',%5,'%6',%7,%8,'%9','%10','%11','%12','%13','%14','%15','%16','%17','%18','%19',%20,'%21','%22',%23,'%24','%25','%26')";
	strSql = strSql.arg(currentPatient->strCheckNum()).arg(currentPatient->strArchivesNum()).arg(currentPatient->strPatientName()).arg(currentPatient->strPatientSex()).arg(
		currentPatient->nPatientAge()).arg(currentPatient->strAgeUnit()).arg(currentPatient->fHeight()).arg(currentPatient->fWeight()).arg(currentPatient->strPatientTell()).arg(currentPatient->strIdentity()).arg
		(currentPatient->strJob()).arg(currentPatient->strAddr()).arg(currentPatient->strCheckType()).arg(currentPatient->strZhuYuanNum()).arg(currentPatient->strBedNum()).arg(
			currentPatient->strSongDoor()).arg(currentPatient->strSongDoctor()).arg(currentPatient->strLinChuangCheck()).arg(currentPatient->strPatientTell()).arg(
				currentPatient->nYuYueTime()).arg(currentPatient->strApplyNum()).arg(currentPatient->strCheckPart()).arg(currentPatient->nPhoneNum()).arg(QString::number(currentPatient->enumPatientStatus())).arg(currentPatient->strFilePath()).arg
				(currentPatient->strSickHistory());
	m_dbUtil->excuteSql(strSql);
}

void  DBOperate::CreatePatientNewReport(const QString& strPatientId, const QString& checkNum)
{

	QString strSql = "INSERT INTO Report (PatientID,CheckNum,ArchivesNum) VALUES ('%1','%2','')";
	strSql = strSql.arg(strPatientId).arg(checkNum);
	m_dbUtil->excuteSql(strSql);
}



void  DBOperate::UpdatePatientCheckStatus(const QString& strCheckNum, PatientStatus status)
{
	QString strSql = "UPDATE patient SET checkStatus = %1 WHERE CheckNum = %2";
	strSql = strSql.arg(QString::number(status)).arg(strCheckNum);
	m_dbUtil->excuteSql(strSql);
}

void  DBOperate::UpdatePatientReport(Pacs_Report* patientReport)
{
	QString strPhotoPath = "";
	SCommon::StringListToString(patientReport->strListImagePath(), ";", strPhotoPath);
	QString selectImagePath = "";
	SCommon::StringListToString(patientReport->strListSelectImagePath(), ";",selectImagePath);
	
	QString strSql = " UPDATE Report SET  ModeSee = '%1',ModeRes='%2',PhotoPath='%3',CheckDoctor='%4',ReportDoctor='%5',CheckDate=%6,ShenHeDoctor='%7',CheckPart='%8',ReportPhotoPath='%9',ReportPath='%10' WHERE CheckNum = '%11' ";
	strSql = strSql.arg(patientReport->strModeSee()).arg(patientReport->strModeRes()).arg(strPhotoPath).arg(patientReport->strCheckDoctor()).arg(
		patientReport->strCheckDoctor()).arg(QString::number(QDateTime::currentDateTime().toSecsSinceEpoch())).arg(patientReport->strShenHeDoctor()).arg(patientReport->strCheckPart()).arg(
		selectImagePath).arg("");
	m_dbUtil->excuteSql(strSql);
}


void  DBOperate::GetPatientReprotInfo(Pacs_Patient *patient)
{
	QString strSql = "SELECT ID,PatientID,CheckNum,ArchivesNum,"\
		"ModeSee,ModeRes,PhotoPath,CheckDoctor,ReportDoctor,CheckDate,"\
		"ShenHeDoctor,CheckPart,ReportPhotoPath,ReportPath,ReportAllImagePath FROM Report WHERE CheckNum = '%1'";
	strSql = strSql.arg(patient->strCheckNum());
	QSqlQuery sqlQuery = m_dbUtil->excuteSql(strSql);
	Pacs_Report* patientReport = patient->patientReport();
	while (sqlQuery.next())
	{
		patientReport->setStrCheckNum(sqlQuery.value("CheckNum").toString());
		patientReport->setModeSee(sqlQuery.value("ModeSee").toString());
		patientReport->setModeRes(sqlQuery.value("ModeRes").toString());
		patientReport->setCheckDoctor(sqlQuery.value("CheckDoctor").toString());
		patientReport->setShenHeDoctor(sqlQuery.value("ShenHeDoctor").toString());
		patientReport->setCheckPart(sqlQuery.value("CheckPart").toString());
		patientReport->setReportDate(sqlQuery.value("CheckDate").toULongLong());
		QString strAllImage = sqlQuery.value("PhotoPath").toString();
		patientReport->SetPatientAllImage(strAllImage.split(";"));
		QString  strSelectImage = sqlQuery.value("ReportPhotoPath").toString();
		patientReport->SetSelectImage(strSelectImage.split(";"));
		
	}
}

void  DBOperate::UpdateReportPath(const QString& strCheckNum, const QString& strFilePath)
{ 
	QString strSql = "UPDATE Report SET ReportPath = '%1' WHERE  CheckNum = '%2'";
	strSql = strSql.arg(strFilePath).arg(strCheckNum);
	m_dbUtil->excuteSql(strSql);
}

void DBOperate::UpdatePatientImage(const QString& strCheckNum, const QString& strFileList)
{
	QString strSql = "UPDATE patient SET imagePath = '%1' WHERE  CheckNum = '%2'";
	strSql = strSql.arg(strFileList).arg(strCheckNum);
	m_dbUtil->excuteSql(strSql);
}


//////////登录相关//////////////////
Pacs_User* DBOperate::onLogin(const QString& strAccount,const QString& strPwd)
{
	QString  strSql = "SELECT a.*,b.* FROM user a  LEFT JOIN doctor b ON a.id = b.userid WHERE (userName = '%1' OR userAccount = '%2' ) AND userPwd = '%3' LIMIT 0,1";
	strSql = strSql.arg(strAccount).arg(strAccount).arg(strPwd);
	QSqlQuery sqlQuery = m_dbUtil->excuteSql(strSql);
	while (sqlQuery.next())
	{
		int userId = sqlQuery.value("id").toInt();
		QString strAccount = sqlQuery.value("userAccount").toString();
		QString strUserName = sqlQuery.value("userName").toString();
		int nSex = sqlQuery.value("userSex").toInt();
		int nType = sqlQuery.value("userType").toInt();
		int nClass = sqlQuery.value("userClass").toInt();
		int docId = sqlQuery.value("docid").toInt();
		QString doctorName = sqlQuery.value("name").toString();
		int doctorType = sqlQuery.value("type").toInt();
		QString doctorDepartment = sqlQuery.value("department").toString();
		QString doctorJob = sqlQuery.value("job").toString();// 职位
		int  doctorPhoneNum = sqlQuery.value("phone").toInt();
		int userid = sqlQuery.value("userid").toInt();
		int doctorLeader = sqlQuery.value("leader").toInt();
		Pacs_User *user = new Pacs_User;
		Pacs_Doctor *doctor = user->GetDoctor();
		user->setStrUserId(userId);
		user->setStrUserAccount(strAccount);
		user->setStrUserName(strUserName);
		user->setNUserType(nType);
		doctor->setDocId(docId);
		doctor->setDoctorName(doctorName);
		doctor->setUserId(userId);
		doctor->setDoctorType(QString::number(doctorType));
		doctor->setDoctorJob(doctorJob);
		doctor->setNdocType(nType);
		doctor->setDoctorLeaderId(doctorLeader);
		return user;
	}
	return NULL;
}

void DBOperate::DestroyKernel()
{
	if (m_instance)
	{
		delete m_instance;
		m_instance = NULL;
	}
}
DBOperate::~DBOperate()
{

}
